package com.demo.file.vo;

import lombok.Data;

@Data
public final class R<T> {
    private static final long serialVersionUID = 2442635434467820038L;
    private static final int SUCCESS = 200;
    private static final int FAIL = 999;

    private int code;

    private String msg;

    private T data;

    private R() {}

    public static <T> R<T> ok() {
        return restResult(SUCCESS, null, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(SUCCESS, null, data);
    }

    public static <T> R<T> ok(String msg, T data) {
        return restResult(SUCCESS, msg, data);
    }

    public static <T> R<T> fail() {
        return restResult(FAIL, null, null);
    }

    public static <T> R<T> fail(String msg) {
        return restResult(FAIL, msg, null);
    }

    public static <T> R<T> fail(T data) {
        return restResult(FAIL, null, data);
    }

    public static <T> R<T> fail(String msg, T data) {
        return restResult(FAIL, msg, data);
    }

    public static <T> R<T> fail(int code, String msg) {
        return restResult(code, msg, null);
    }

    private static <T> R<T> restResult(int code, String msg, T data) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return this.code == SUCCESS ? Boolean.TRUE : Boolean.FALSE;
    }

}
